package com.woniuxy.lab.woniu_lab_boss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.woniuxy.lab.woniu_lab_boss.controller.form.AddBaseForm;
import com.woniuxy.lab.woniu_lab_boss.exception.BaseException;
import com.woniuxy.lab.woniu_lab_boss.model.Base;
import com.woniuxy.lab.woniu_lab_boss.dao.BaseDao;
import com.woniuxy.lab.woniu_lab_boss.service.BaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lab.woniu_lab_boss.service.dto.BaseDto;
import com.woniuxy.lab.woniu_lab_boss.service.dto.ParentBaseDto;
import com.woniuxy.lab.woniu_lab_boss.service.dto.BaseDto;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-23
 */
@Service
public class BaseServiceImpl extends ServiceImpl<BaseDao, Base> implements BaseService {

    public static final String NAME = "name";

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBase(AddBaseForm addBaseForm) {
        //去重
        Base base = getOne(new QueryWrapper<Base>().eq(NAME, addBaseForm.getName()));
        if (!ObjectUtil.isEmpty(base)){
            throw new BaseException(IMAGE_IS_EXISTS);
        }
        if (!"0".equals(addBaseForm.getParentId())){
            //判断父ID是否存在
            Base parentBase = getById(addBaseForm.getParentId());
            if (ObjectUtil.isEmpty(parentBase)){
                throw new BaseException(PARENT_IMAGE_IS_NOT_EXISTS);
            }
            //判断父ID是否是父镜像
            if(!parentBase.getParentId().equals(0)){
                throw new BaseException(PARENT_IMAGE_IS_NOT_PARENT);

            }
        }
        //TODO 拉取镜像并获取镜像ID
        //存入数据库
        Base base1 = BeanUtil.copyProperties(addBaseForm, Base.class);
        //base1.setBaseId("");
        save(base1);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBase(Integer baseId) {
        //判断是否为空
        Base base = getById(baseId);
        if (ObjectUtil.isEmpty(base)){
            throw new BaseException(IMAGE_IS_NOT_EXISTS);
        }
        //TODO 先判断是否存在运行的容器 然后关闭容器，再删除容器，再删除镜像
        //数据库删除容器
        removeById(baseId);
    }

    @Override
    public List<ParentBaseDto> queryBase() {
        List<Base> bases = list();
        //查出父镜像
        List<Base> parentBases = new ArrayList<>();
        for (Base base : bases) {
            if (base.getParentId()==0){
                parentBases.add(base);
            }
        }
        //parentBases转换为ParentBaseDto
        List<ParentBaseDto> parentBaseDtos = BeanUtil.copyToList(parentBases, ParentBaseDto.class);
        //bases转换为BaseDto
        List<BaseDto> baseDtos = BeanUtil.copyToList(bases, BaseDto.class);
        for (ParentBaseDto parentBaseDto : parentBaseDtos) {
            List<BaseDto> images = parentBaseDto.getImages();
            for (BaseDto baseDto : baseDtos) {
                //子找父
                if (baseDto.getParentId().equals(parentBaseDto.getId())){
                    //初始化list
                    if (images==null || images.size()==0){
                        images=new ArrayList<>();
                    }
                    images.add(baseDto);
                }
            }
            //写入parentBaseDtos
            parentBaseDto.setImages(images);
        }
        return parentBaseDtos;
    }
}
